using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using Beetle.Controller;

internal class az
{
	private static Dictionary<FieldInfo, GetValueHandler> m_a = new Dictionary<FieldInfo, GetValueHandler>();

	private static Dictionary<FieldInfo, SetValueHandler> m_b = new Dictionary<FieldInfo, SetValueHandler>();

	private static Dictionary<PropertyInfo, GetValueHandler> m_c = new Dictionary<PropertyInfo, GetValueHandler>();

	private static Dictionary<PropertyInfo, SetValueHandler> m_d = new Dictionary<PropertyInfo, SetValueHandler>();

	private static Dictionary<MethodInfo, FastMethodHandler> e = new Dictionary<MethodInfo, FastMethodHandler>();

	private static Dictionary<Type, ObjectInstanceHandler> f = new Dictionary<Type, ObjectInstanceHandler>();

	public static GetValueHandler d(FieldInfo A_0)
	{
		if (az.m_a.ContainsKey(A_0))
		{
			return az.m_a[A_0];
		}
		lock (typeof(az))
		{
			if (az.m_a.ContainsKey(A_0))
			{
				return az.m_a[A_0];
			}
			GetValueHandler getValueHandler = c(A_0);
			az.m_a.Add(A_0, getValueHandler);
			return getValueHandler;
		}
	}

	private static GetValueHandler c(FieldInfo A_0)
	{
		DynamicMethod dynamicMethod = new DynamicMethod("", typeof(object), new Type[1] { typeof(object) }, A_0.DeclaringType);
		ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
		iLGenerator.Emit(OpCodes.Ldarg_0);
		iLGenerator.Emit(OpCodes.Ldfld, A_0);
		a(iLGenerator, A_0.FieldType);
		iLGenerator.Emit(OpCodes.Ret);
		return (GetValueHandler)dynamicMethod.CreateDelegate(typeof(GetValueHandler));
	}

	public static SetValueHandler b(FieldInfo A_0)
	{
		if (az.m_b.ContainsKey(A_0))
		{
			return az.m_b[A_0];
		}
		lock (typeof(az))
		{
			if (az.m_b.ContainsKey(A_0))
			{
				return az.m_b[A_0];
			}
			SetValueHandler setValueHandler = a(A_0);
			az.m_b.Add(A_0, setValueHandler);
			return setValueHandler;
		}
	}

	private static SetValueHandler a(FieldInfo A_0)
	{
		DynamicMethod dynamicMethod = new DynamicMethod("", null, new Type[2]
		{
			typeof(object),
			typeof(object)
		}, A_0.DeclaringType);
		ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
		iLGenerator.Emit(OpCodes.Ldarg_0);
		iLGenerator.Emit(OpCodes.Ldarg_1);
		b(iLGenerator, A_0.FieldType);
		iLGenerator.Emit(OpCodes.Stfld, A_0);
		iLGenerator.Emit(OpCodes.Ret);
		return (SetValueHandler)dynamicMethod.CreateDelegate(typeof(SetValueHandler));
	}

	public static SetValueHandler d(PropertyInfo A_0)
	{
		if (az.m_d.ContainsKey(A_0))
		{
			return az.m_d[A_0];
		}
		lock (typeof(az))
		{
			if (az.m_d.ContainsKey(A_0))
			{
				return az.m_d[A_0];
			}
			SetValueHandler setValueHandler = c(A_0);
			az.m_d.Add(A_0, setValueHandler);
			return setValueHandler;
		}
	}

	private static SetValueHandler c(PropertyInfo A_0)
	{
		DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, null, new Type[2]
		{
			typeof(object),
			typeof(object)
		}, A_0.DeclaringType.Module);
		ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
		iLGenerator.Emit(OpCodes.Ldarg_0);
		iLGenerator.Emit(OpCodes.Ldarg_1);
		b(iLGenerator, A_0.PropertyType);
		iLGenerator.EmitCall(OpCodes.Callvirt, A_0.GetSetMethod(), null);
		iLGenerator.Emit(OpCodes.Ret);
		return (SetValueHandler)dynamicMethod.CreateDelegate(typeof(SetValueHandler));
	}

	public static GetValueHandler b(PropertyInfo A_0)
	{
		if (az.m_c.ContainsKey(A_0))
		{
			return az.m_c[A_0];
		}
		lock (typeof(az))
		{
			if (az.m_c.ContainsKey(A_0))
			{
				return az.m_c[A_0];
			}
			GetValueHandler getValueHandler = a(A_0);
			az.m_c.Add(A_0, getValueHandler);
			return getValueHandler;
		}
	}

	private static GetValueHandler a(PropertyInfo A_0)
	{
		DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, typeof(object), new Type[1] { typeof(object) }, A_0.DeclaringType.Module);
		ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
		iLGenerator.Emit(OpCodes.Ldarg_0);
		iLGenerator.EmitCall(OpCodes.Callvirt, A_0.GetGetMethod(), null);
		a(iLGenerator, A_0.PropertyType);
		iLGenerator.Emit(OpCodes.Ret);
		return (GetValueHandler)dynamicMethod.CreateDelegate(typeof(GetValueHandler));
	}

	public static FastMethodHandler b(MethodInfo A_0)
	{
		FastMethodHandler fastMethodHandler = null;
		if (e.ContainsKey(A_0))
		{
			return e[A_0];
		}
		lock (typeof(az))
		{
			if (e.ContainsKey(A_0))
			{
				return e[A_0];
			}
			fastMethodHandler = a(A_0);
			e.Add(A_0, fastMethodHandler);
			return fastMethodHandler;
		}
	}

	private static FastMethodHandler a(MethodInfo A_0)
	{
		DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, typeof(object), new Type[2]
		{
			typeof(object),
			typeof(object[])
		}, A_0.DeclaringType.Module);
		ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
		ParameterInfo[] parameters = A_0.GetParameters();
		Type[] array = new Type[parameters.Length];
		for (int num = 0; num < array.Length; num++)
		{
			if (parameters[num].ParameterType.IsByRef)
			{
				array[num] = parameters[num].ParameterType.GetElementType();
			}
			else
			{
				array[num] = parameters[num].ParameterType;
			}
		}
		LocalBuilder[] array2 = new LocalBuilder[array.Length];
		for (int num2 = 0; num2 < array.Length; num2++)
		{
			array2[num2] = iLGenerator.DeclareLocal(array[num2], pinned: true);
		}
		for (int num3 = 0; num3 < array.Length; num3++)
		{
			iLGenerator.Emit(OpCodes.Ldarg_1);
			a(iLGenerator, num3);
			iLGenerator.Emit(OpCodes.Ldelem_Ref);
			b(iLGenerator, array[num3]);
			iLGenerator.Emit(OpCodes.Stloc, array2[num3]);
		}
		if (!A_0.IsStatic)
		{
			iLGenerator.Emit(OpCodes.Ldarg_0);
		}
		for (int num4 = 0; num4 < array.Length; num4++)
		{
			if (parameters[num4].ParameterType.IsByRef)
			{
				iLGenerator.Emit(OpCodes.Ldloca_S, array2[num4]);
			}
			else
			{
				iLGenerator.Emit(OpCodes.Ldloc, array2[num4]);
			}
		}
		if (A_0.IsStatic)
		{
			iLGenerator.EmitCall(OpCodes.Call, A_0, null);
		}
		else
		{
			iLGenerator.EmitCall(OpCodes.Callvirt, A_0, null);
		}
		if (A_0.ReturnType == typeof(void))
		{
			iLGenerator.Emit(OpCodes.Ldnull);
		}
		else
		{
			a(iLGenerator, A_0.ReturnType);
		}
		for (int num5 = 0; num5 < array.Length; num5++)
		{
			if (parameters[num5].ParameterType.IsByRef)
			{
				iLGenerator.Emit(OpCodes.Ldarg_1);
				a(iLGenerator, num5);
				iLGenerator.Emit(OpCodes.Ldloc, array2[num5]);
				if (array2[num5].LocalType.IsValueType)
				{
					iLGenerator.Emit(OpCodes.Box, array2[num5].LocalType);
				}
				iLGenerator.Emit(OpCodes.Stelem_Ref);
			}
		}
		iLGenerator.Emit(OpCodes.Ret);
		return (FastMethodHandler)dynamicMethod.CreateDelegate(typeof(FastMethodHandler));
	}

	public static ObjectInstanceHandler b(Type A_0)
	{
		if (f.ContainsKey(A_0))
		{
			return f[A_0];
		}
		lock (typeof(az))
		{
			if (f.ContainsKey(A_0))
			{
				return f[A_0];
			}
			ObjectInstanceHandler objectInstanceHandler = a(A_0);
			f.Add(A_0, objectInstanceHandler);
			return objectInstanceHandler;
		}
	}

	private static ObjectInstanceHandler a(Type A_0)
	{
		DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, A_0, null, A_0.Module);
		ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
		iLGenerator.DeclareLocal(A_0, pinned: true);
		iLGenerator.Emit(OpCodes.Newobj, A_0.GetConstructor(new Type[0]));
		iLGenerator.Emit(OpCodes.Stloc_0);
		iLGenerator.Emit(OpCodes.Ldloc_0);
		iLGenerator.Emit(OpCodes.Ret);
		return (ObjectInstanceHandler)dynamicMethod.CreateDelegate(typeof(ObjectInstanceHandler));
	}

	private static void b(ILGenerator A_0, Type A_1)
	{
		if (A_1.IsValueType)
		{
			A_0.Emit(OpCodes.Unbox_Any, A_1);
		}
		else
		{
			A_0.Emit(OpCodes.Castclass, A_1);
		}
	}

	private static void a(ILGenerator A_0, Type A_1)
	{
		if (A_1.IsValueType)
		{
			A_0.Emit(OpCodes.Box, A_1);
		}
	}

	private static void a(ILGenerator A_0, int A_1)
	{
		switch (A_1)
		{
		case -1:
			A_0.Emit(OpCodes.Ldc_I4_M1);
			return;
		case 0:
			A_0.Emit(OpCodes.Ldc_I4_0);
			return;
		case 1:
			A_0.Emit(OpCodes.Ldc_I4_1);
			return;
		case 2:
			A_0.Emit(OpCodes.Ldc_I4_2);
			return;
		case 3:
			A_0.Emit(OpCodes.Ldc_I4_3);
			return;
		case 4:
			A_0.Emit(OpCodes.Ldc_I4_4);
			return;
		case 5:
			A_0.Emit(OpCodes.Ldc_I4_5);
			return;
		case 6:
			A_0.Emit(OpCodes.Ldc_I4_6);
			return;
		case 7:
			A_0.Emit(OpCodes.Ldc_I4_7);
			return;
		case 8:
			A_0.Emit(OpCodes.Ldc_I4_8);
			return;
		}
		if (A_1 > -129 && A_1 < 128)
		{
			A_0.Emit(OpCodes.Ldc_I4_S, (sbyte)A_1);
		}
		else
		{
			A_0.Emit(OpCodes.Ldc_I4, A_1);
		}
	}
}
